package com.googlecode.jasperlib.internal;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.googlecode.jasperlib.ClassLoaderInterface;

public class ServletContextDelegate implements ServletContext {
    private static final Log LOG = LogFactory.getLog(ServletContextDelegate.class);

    private final Map<String, Object> attributes;
    private final ClassLoaderInterface classLoaderInterface;

    public ServletContextDelegate(ClassLoaderInterface classLoaderInterface) {
        this(classLoaderInterface, new HashMap<String, Object>());
    }

    public ServletContextDelegate(ClassLoaderInterface classLoaderInterface, Map<String, Object> attributes) {
        this.attributes = attributes;
        this.classLoaderInterface = classLoaderInterface;
    }

    public Object getAttribute(String name) {
        return attributes.get(name);
    }

    public Enumeration getAttributeNames() {
        return Collections.enumeration(attributes.keySet());
    }

    public ServletContext getContext(String uripath) {
        return null;
    }

    public String getInitParameter(String name) {
        return null;
    }

    public Enumeration getInitParameterNames() {
        return null;
    }

    public int getMajorVersion() {
        return 0;
    }

    public String getMimeType(String file) {
        return null;
    }

    public int getMinorVersion() {
        return 0;
    }

    public RequestDispatcher getNamedDispatcher(String name) {
        return null;
    }

    public String getRealPath(String path) {
        return null;
    }

    public RequestDispatcher getRequestDispatcher(String path) {
        return null;
    }

    public URL getResource(String path) throws MalformedURLException {
        return classLoaderInterface.getResource(path);
    }

    public InputStream getResourceAsStream(String path) {
        try {
            return classLoaderInterface.getResourceAsStream(path);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public Set getResourcePaths(String path) {
        return null;
    }

    public String getServerInfo() {
        return "Fake Servlet Context";
    }

    public Servlet getServlet(String name) throws ServletException {
        return null;
    }

    public String getServletContextName() {
        return null;
    }

    public Enumeration getServletNames() {
        return null;
    }

    public Enumeration getServlets() {
        return null;
    }

    public void log(String msg) {
        LOG.debug(msg);
    }

    public void log(Exception exception, String msg) {
        LOG.error(msg, exception);
    }

    public void log(String msg, Throwable throwable) {
        LOG.error(msg, throwable);
    }

    public void removeAttribute(String name) {
        attributes.remove(name);
    }

    public void setAttribute(String name, Object object) {
        attributes.put(name, object);
    }
}
